﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.Mvc;
using GWEB.WebBase.Data;
using GWEB.WebBase.Entity;
using GWEB.WebBase.Utility;
using GWEB.WebFrame.Public;
using Newtonsoft.Json;

namespace GWEB.WebFrame.Customer
{
    public class FollowupListController : PageInfoBase
    {
        //提醒
        [ActionFilter(IsJson =true)]
        public JsonResult Remind(String id, String keyCode,String remark)
        {
            if (!Fun.IDVerify(id, keyCode))
                return BasePage.Json(ResultData.Error(WebMessage.NoGrant));
            String date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            String cid = id;
            String sql = String.Format("select top 1 AdviserId,ReceiveDate,LeagueId From cu_Cus_Distribution where cid={0} and state=1 order by id desc", cid);
            DataRow dr = BasePage.bd.ExecuteRow(sql);
            if (dr == null) return BasePage.Json(ResultData.Error("该客户您未领取过"));

            Dictionary<String, Object> data = new Dictionary<String, Object>();
            data.Add("AdviserId", dr["AdviserId"]);
            data.Add("ReceiveDate", dr["ReceiveDate"]);
            data.Add("LeagueId", dr["LeagueId"]);
            data.Add("CreateDate", date);
            data.Add("adMode", 1);
            data.Add("cid", cid);
            data.Add("Cusgrade",90);
            data.Add("Remark", remark);
            data.Add("CourseState", 0);
           
            sql= BaseData.MapToSql("cu_Cus_followup", "id", "-1", data);
            bool t = BasePage.bd.ExecuteSQL(sql);
            return BasePage.Json(t ? ResultData.OK("提醒成功") : ResultData.Error("提醒失败"));
        }
        //试听退费 未试听 已收费 未入账=》删除流水且收费状态改0
        [ActionFilter(IsJson = true)]
        public JsonResult AuditionReturn(String id, String keyCode)
        {
            if (!Fun.IDVerify(id, keyCode))
                return BasePage.Json(ResultData.Error(WebMessage.NoGrant));
            int total = 2;
            String[] sqls = new string[2];
            sqls[0] = String.Format("delete cu_account where state=0 and followupId in ({0})", id);
            sqls[1] = String.Format("update cu_Cus_followup set isCharge=0 where courseState=0 and isCharge=1 and id in ({0})", id);
            bool t = BasePage.bd.ExecuteSQL(sqls, total);
            return BasePage.Json(t ? ResultData.OK("退费成功") : ResultData.Error("退费失败"));
        }
        //退订金
        [ActionFilter(IsJson = true)]
        public JsonResult DepositReturn(String id, String keyCode)
        {
            if (!Fun.IDVerify(id, keyCode))
                return BasePage.Json(ResultData.Error(WebMessage.NoGrant));
            int total = 2;
            String[] sqls = new string[2];
            sqls[0] = String.Format("delete cu_account where state=0 and followupId in ({0})", id);
            sqls[1] = String.Format(@"update cu_customers set custState=3 where custState=5 
and isnull((select sum(account) from cu_Account where FlowingType in ('00101001', '00201001') and state <= 2 and cid = cu_customers.id), 0) <= 0
and id in (select cid from cu_Cus_followup where id = {0})
 ", id);
            bool t = BasePage.bd.ExecuteSQL(sqls, total);
            return BasePage.Json(t ? ResultData.OK("退订金成功") : ResultData.Error("退订金失败"));
        }
    }
}